﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.Collections;

public partial class Management_CourseManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            bindgrid();
    }
    void bindgrid()
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        ArrayList drarr = new ArrayList();
        ArrayList recoarr = new ArrayList();
        //DataSet ds2 = new DataSet();
        String getCid = HttpContext.Current.Request.QueryString["cid"];
        int i;
        ds = db.GetDataSet("select cid,logo,cname,introduction,department,keyword from mr_course");
        DataTable dt = new DataTable();
        dt.Columns.Add("cid");
        dt.Columns.Add("image");
        dt.Columns.Add("cname");
        dt.Columns.Add("introduction");
        dt.Columns.Add("department");
        dt.Columns.Add("keyword");
        dt.Columns.Add("编辑");
        dt.Columns.Add("删除");
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["cid"];
            dr[2] = ds.Tables[0].Rows[i]["cname"];
            dr[3] = ds.Tables[0].Rows[i]["introduction"];
            dr[4] = ds.Tables[0].Rows[i]["department"];
            dr[5] = ds.Tables[0].Rows[i]["keyword"];
            dr[1] = ResolveUrl(ds.Tables[0].Rows[i]["logo"].ToString());
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;
        GridView1.AllowPaging = true;
        GridView1.PageSize = 4;
        GridView1.DataBind();
        //GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        GridView1.Columns[3].ItemStyle.Width = 200;
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridView1.Columns[0].ControlStyle.Width = 20;
        GridView1.Columns[1].ControlStyle.Width = 50;
        GridView1.Columns[2].ControlStyle.Width = 40;
        GridView1.Columns[3].ControlStyle.Width = 150;
        GridView1.Columns[4].ControlStyle.Width = 20;
        GridView1.Columns[5].ControlStyle.Width = 100;
        bindgrid();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DBaccess db = new DBaccess();
        string name;
        string introduction;
        int did;
        string keyword;
        int id = e.RowIndex;
        GridViewRow gvr = GridView1.Rows[id];
        int index =int.Parse(gvr.Cells[0].Text);
        name = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
        introduction = (GridView1.Rows[e.RowIndex].Cells[3].Controls[0] as TextBox).Text;
        did = int.Parse((GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text);
        keyword = (GridView1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text;
        string sql = "update mr_course set cname='" + name + "', introduction='" + introduction + "',department='" + did + "',keyword = '" + keyword + "' where cid='" + index + "'";
        int read = db.ExecSql(sql);
        Response.Redirect("./CourseManagement.aspx");
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.Columns[0].ControlStyle.Width = 20;
        GridView1.Columns[1].ControlStyle.Width = 50;
        GridView1.Columns[2].ControlStyle.Width = 40;
        GridView1.Columns[3].ControlStyle.Width = 200;
        GridView1.Columns[4].ControlStyle.Width = 50;
        bindgrid();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        bindgrid();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string text;
        DBaccess db = new DBaccess();
        text = GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
        int deleteview = db.ExecSql("delete from mr_course where cid = '" + text + "'");
        Response.Redirect("./CourseManagement.aspx");
    }
    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];
        TableCell contactMid = selectedRow.Cells[0];
        string contact = contactMid.Text;
        DBaccess db = new DBaccess();
        if (e.CommandName == "delete")
        {
         
            int deleteview = db.ExecSql("delete from mr_course where tid = '" + contact + "'");
            Response.Redirect("./CourseManagement.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string search = TextBox1.Text;
        DBaccess db = new DBaccess();

        DataTable dt = new DataTable();
        DataSet ds1 = new DataSet();
        ArrayList drarr = new ArrayList();
        ArrayList recoarr = new ArrayList();
        //DataSet ds2 = new DataSet();
        String getCid = HttpContext.Current.Request.QueryString["cid"];
        int i;
        DataSet ds = db.GetDataSet("select cid,logo,cname,introduction,department,keyword from mr_course where cname like '"+search+"'");
        dt.Columns.Add("cid");
        dt.Columns.Add("image");
        dt.Columns.Add("cname");
        dt.Columns.Add("introduction");
        dt.Columns.Add("department");
        dt.Columns.Add("keyword");
        dt.Columns.Add("编辑");
        dt.Columns.Add("删除");
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["cid"];
            dr[2] = ds.Tables[0].Rows[i]["cname"];
            dr[3] = ds.Tables[0].Rows[i]["introduction"];
            dr[4] = ds.Tables[0].Rows[i]["department"];
            dr[5] = ds.Tables[0].Rows[i]["keyword"];
            dr[1] = ResolveUrl(ds.Tables[0].Rows[i]["logo"].ToString());
            dt.Rows.Add(dr);
        }

        GridView1.DataSource = dt;
        GridView1.AllowPaging = true;
        GridView1.PageSize = 4;
        GridView1.DataBind();
        GridView1.Columns[3].ItemStyle.Width = 200;
    }
}